/*
 * Copyright (c) 2021, 2023 Oracle and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.helidon.metrics.api;

import java.time.Duration;

import io.helidon.config.Config;
import io.helidon.config.ConfigSources;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class TestKpiMetricsConfig {

    private static final Config TEST_CONFIG = Config.just(ConfigSources.classpath("kpiconfig.yaml"));

    @Test
    void checkExtendedEnabled() {
        KeyPerformanceIndicatorMetricsConfig settings =
                KeyPerformanceIndicatorMetricsConfig.create(TEST_CONFIG.get("extended-enabled"));
        assertThat("KPI extended setting", settings.extended(), is(true));
        assertThat("Long-running threshold", settings.longRunningRequestThreshold(),
                   is(Duration.parse(KeyPerformanceIndicatorMetricsConfig.LONG_RUNNING_REQUESTS_THRESHOLD_DEFAULT)));
    }

    @Test
    void checkExtendedDisabled() {
        KeyPerformanceIndicatorMetricsConfig settings =
                KeyPerformanceIndicatorMetricsConfig.create(TEST_CONFIG.get("extended-disabled"));
        assertThat("KPI extended setting", settings.extended(), is(false));
        assertThat("Long-running threshold", settings.longRunningRequestThreshold(),
                   is(Duration.parse(KeyPerformanceIndicatorMetricsConfig.LONG_RUNNING_REQUESTS_THRESHOLD_DEFAULT)));
    }

    @Test
    void longRunningSet() {
        KeyPerformanceIndicatorMetricsConfig settings =
                KeyPerformanceIndicatorMetricsConfig.create(TEST_CONFIG.get("long-running-set"));
        assertThat("KPI extended setting", settings.extended(),
                   is(false));
        assertThat("Long-running threshold", settings.longRunningRequestThreshold(), is(Duration.ofSeconds(3)));
    }

    @Test
    void bothSet() {
        KeyPerformanceIndicatorMetricsConfig settings =
                KeyPerformanceIndicatorMetricsConfig.create(TEST_CONFIG.get("both"));
        assertThat("KPI extended setting", settings.extended(), is(true));
        assertThat("Long-running threshold", settings.longRunningRequestThreshold(), is(Duration.ofSeconds(4)));
    }
}
